package pkg1.controller;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import pkg1.entity.PageBean;
import pkg1.entity.Seller;
import pkg1.entity.SellerQuery;
import pkg1.service.SellerServiceInter;

@Controller
@RequestMapping("/seller")
public class SellerController {
	@Autowired
	private SellerServiceInter sellerSer;

	@RequestMapping(value = "addSeller", method = RequestMethod.GET)
	public String addSeller(Seller seller, HttpSession session) {
		seller.setId(sellerIdGen(session));
		seller.setDelFlg("0");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		seller.setCreateTime(sdf.format(new Date()));
		seller.setUpdateTime(sdf.format(new Date()));
		if (sellerSer.addSeller(seller)) {
			return "redirect:/html/query.html";
		} else {
			return "redirect:/html/add.html";
		}
	}

	private String sellerIdGen(HttpSession session) {
		String username = session.getAttribute("username").toString();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		return username.subSequence(0, 1).toString().toUpperCase() + sdf.format(new Date());
	}

	@ResponseBody
	@RequestMapping(value = "querySeller", method = RequestMethod.GET, produces = "application/json;charset=utf-8")
	public PageBean<Seller> getPage(int currentPage, SellerQuery query) {
		return sellerSer.getPage(currentPage, query);
	}

	@ResponseBody
	@RequestMapping(value = "deleteSeller", method = RequestMethod.GET, produces = "application/json;charset=utf-8")
	public void deleteSeller(String[] ids) {
		sellerSer.deleteMultiIds(ids);
	}

	@RequestMapping(value = "updateSeller", method = RequestMethod.GET)
	public String updateMember(Seller seller) {
		seller.setDelFlg("0");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		seller.setUpdateTime(sdf.format(new Date()));
		if (sellerSer.updateSeller(seller)) {
			System.out.println("memSer success");
			return "redirect:/html/query.html";
		} else {
			System.out.println("memSer failed");
			return "redirect:/html/update.html";
		}
	}
}
